package dao.daoImpl;

import dao.StuDao;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import pojo.Student;
import utils.JDBCUtils;

import java.sql.SQLException;

public class StuDaoImpl implements StuDao {
    @Override
    public Student findStuById(String id) throws SQLException {
        String sql =" SELECT * FROM student WHERE stu_id = ?";
        QueryRunner queryRunner = new QueryRunner(JDBCUtils.getDataSource());
        Student stu =  queryRunner.query(sql, new BeanHandler<Student>(Student.class),id);
        return stu;
    }

    @Override
    public void updateStu(Student stu) throws SQLException {
        String sql ="  UPDATE  `student`  SET \n" +
                    "  `stu_col_info` = ?,`stu_org_info` = ?,`stu_youth_info` = ?,`stu_lib_info` = ?,\n" +
                    "   `stu_off_info` = ?,`stu_fina_info` = ?,`stu_manage_info` = ?,`stu_domi_info` = ?\n" +
                    "   WHERE `stu_id` = ?";
        QueryRunner queryRunner = new QueryRunner(JDBCUtils.getDataSource());
        Object [] params = {stu.getStu_col_info(),stu.getStu_org_info(),stu.getStu_youth_info(),stu.getStu_lib_info(),
                            stu.getStu_off_info(),stu.getStu_fina_info(),stu.getStu_manage_info(),stu.getStu_domi_info(),
                            stu.getStu_id()};
        queryRunner.update(sql,params);
    }
}
